package com.study.zkyxgs1.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回类
 * @author weixiaochun
 * @date 2022/6/14 21:32
 */
@Data
public class ResultMap {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    /**
     * 构造方法私有化
     */
    private ResultMap(){}

    /**
     * 成功静态方法
     * @return
     */
    public static ResultMap ok(){
        ResultMap r = new ResultMap();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("请求成功");
        return r;
    }

    /**
     * 成功静态方法
     * @param message 成功消息
     * @return
     */
    public static ResultMap ok(String message){
        ResultMap r = new ResultMap();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage(message);
        return r;
    }

    /**
     * 失败静态方法
     * @return
     */
    public static ResultMap error(){
        ResultMap r = new ResultMap();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("请求失败");
        return r;
    }

    /**
     * 失败静态方法
     * @param message 失败消息
     * @return
     */
    public static ResultMap error(String message){
        ResultMap r = new ResultMap();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage(message);
        return r;
    }

    public ResultMap success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public ResultMap message(String message){
        this.setMessage(message);
        return this;
    }
    public ResultMap code(Integer code){
        this.setCode(code);
        return this;
    }
    public ResultMap data(String key, Object value){
        this.data.put(key, value);
        return this;
    }
    public ResultMap data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
